<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="item.cnname">
        <el-input v-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.dateValue"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="createTime" label="创建日期" width="160" />
      <el-table-column prop="lostName" label="名称" width="120" />
      <el-table-column prop="gleanMan" label="拾到人" width="120" />
      <el-table-column prop="enterMan" label="记录人" width="120" />
      <el-table-column prop="originDepart" label="拾到地址" width="240" />
      <el-table-column prop="fileRandomId" label="附件" width="120">
        <template #default="scope">
          <div style="display: flex; align-items: center; height: 46px">
            <el-image
              preview-teleported="true"
              :src="singleUrl(scope.row.fileRandomId)"
              :preview-src-list="arrayUrl(scope.row.fileRandomId)"
              fit="fill"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="lostOwnerName" label="失主" width="120" />
      <el-table-column prop="tel" label="失主联系方式" width="120" />
      <el-table-column prop="drawer" label="领取人" width="120" />
      <el-table-column prop="drawCard" label="领取人证件" width="120" />
      <el-table-column prop="drawTel"  label="领取人联系方式" width="120" />
      <el-table-column prop="provider" label="发放人" width="120" />
      <el-table-column prop="provideDateTime" label="发放时间" width="160" />
      <el-table-column prop="fileDesc" label="物件描述" width="120" />
      <el-table-column prop="claim" label="是否完成" width="120">
        <template #default="scope">
          <span>{{$t('msg.common.'+scope.row.claim)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="statusDesc" label="当前状态" width="120">
        <template #default="scope">
          <el-select v-model="scope.row.statusDesc" placeholder="" name="statusDesc" disabled >
            <el-option label="创建" value="CREATE" />
            <el-option label="申请" value="APPLY" />
            <el-option label="发布" value="PUBLIC" />
            <el-option label="审核" value="AUDIT" />
            <el-option label="结束" value="END" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
          <el-button v-if="scope.row.statusDesc == 'APPLY'" text size="small" @click="handleClick(scope.row)">编辑</el-button>
           <el-button v-if="scope.row.statusDesc == 'PUBLIC'" text type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      v-model="dialogVisible"
      title="审核"
      width="30%"
    >
      <el-form v-model="lostEntity" ref="lostRef">
        <el-form-item label="名称">
          <el-input v-model="lostEntity.lostName" disabled></el-input>
        </el-form-item>
        <el-form-item label="失主" prop="lostOwnerName">
          <el-input v-model="lostEntity.lostOwnerName"></el-input>
        </el-form-item>
        <el-form-item label="领取人" prop="drawer">
          <el-input v-model="lostEntity.drawer"></el-input>
        </el-form-item>
        <el-form-item label="领取人证件号" >
          <el-input v-model="lostEntity.drawCard"></el-input>
        </el-form-item>
        <el-form-item label="领取人联系方式" prop="drawTel">
          <el-input type="number" v-model="lostEntity.drawTel"></el-input>
        </el-form-item>
        <el-form-item label="发放人" prop="provider">
          <el-input v-model="lostEntity.provider"></el-input>
        </el-form-item>
        <el-form-item label="领取时间" required>
          <el-col :span="12">
            <el-form-item>
              <el-date-picker
                v-model="lostEntity.provideDateTime"
                type="datetime"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onAudit"
            >审核</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { list, audit, deleteById } from '@/api/lost'
import { dateToString, dateToNext, dateTimeToString } from '@/utils/date'
import { singleUrl, arrayUrl } from '@/utils/file'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'

const tableDatas = ref({})
const dialogVisible = ref(false)
const lostRef = ref(null)

const loading = ref(false)

const i18n = useI18n()
const lostRules = ref({
  lostOwnerName: [
    {
      required: true,
      trigger: 'blur',
      message: '失主不能为空'
    }
  ],
  drawer: [
    {
      required: true,
      trigger: 'blur',
      message: '领取人员不能为空'
    }
  ],
  drawTel: [
    {
      required: true,
      trigger: 'blur',
      message: '领取人电话不能为空'
    }
  ],
  provider: [
    {
      required: true,
      trigger: 'blur',
      message: '发放人员不能为空'
    }
  ]
})

const handleClose = () => {
  dialogVisible.value = false
}
const searchPage = ref({
  items: [
    {
      name: 'lostName',
      cnname: '名称',
      itemType: 'STRING',
      sign: 'like',
      value: ''
    },
    {
      name: 'createTime1',
      cnname: '开始时间',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-7),
      dateValue: dateToNext(-7)
    },
    {
      name: 'createTime2',
      cnname: '结束时间',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: dateToNext(0)
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const tableData = ref([])

const lostEntity = ref({})

const searchFunction = async () => {
  list(searchPage.value).then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'lostName',
        cnname: '名称',
        itemType: 'STRING',
        sign: 'like',
        value: ''
      },
      {
        name: 'createTime1',
        cnname: '开始时间',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-7),
        dateValue: dateToNext(-7)
      },
      {
        name: 'createTime2',
        cnname: '结束时间',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: dateToNext(0)
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const handleClick = row => {
  dialogVisible.value = true
  lostEntity.value = row
  lostEntity.value.provideDateTime = dateTimeToString(0)
}

const onAudit = () => {
  const valid = lostEntity.value
  if (valid.lostOwnerName && valid.drawer && valid.drawTel && valid.provider) {
    lostEntity.value.statusDesc = 'END'
    audit(lostEntity.value).then(res => {
      // 察看是否双向绑定了，如果没有需要执行查询操作
      ElMessage.success('成功')
      dialogVisible.value = false
    }).catch(err => {
      ElMessage.error('失败' || err.message)
    })
  } else {
    ElMessage.info('内容填写不全')
  }
  // lostRef.value.validate(valid => {
  //   if (!valid) return
  //   lostEntity.value.statusDesc = 'END'
  // })
}

const handleDelete = row => {
  loading.value = true
  deleteById(row.id).then(res => {
    loading.value = false
    ElMessage.success('成功')
    searchFunction()
  }).catch(err => {
    loading.value = false
    ElMessage.error(err.message)
  })
}
</script>

<style scope>
.search-container {
  width: 100%;
  height: 100px;
}
</style>
